import React from 'react';
import Router from './router';
import { ll } from './utils/storage';
import { useLocation, useNavigate, Navigate } from 'react-router-dom';

const App: React.FC = () => {
  return <Router></Router>
}


const beforeEach = (Comp: React.FC) => {
  return () => {
    // 拦截逻辑
    const token = ll.getItem('token');
    const { pathname } = useLocation()
    if(token) {
      // 1. 已经登录
      if(pathname === '/login') {
        // 1-1 访问登录页面
        return <Navigate to="/"></Navigate>
      } else {
        // 1-2 访问非登录页面
        return <Comp></Comp>
      }
    } else {
      // 2. 未登录
      if(pathname === '/login') {
        // 2-1 访问登录页面
        return <Comp></Comp>
      } else {
        // 2-1 访问非登录页面
        return <Navigate to="/login"></Navigate>
      }
    }
    return <Comp></Comp>
  }
}

export default beforeEach(App)